<?php
class Admin_Model_CategoryFeature extends Zend_Db_Table_Abstract {
	protected $_name = 'category_feature';
	protected $_primary = array('categoryId', 'featureId');
	protected $_dependentTables = array('Admin_Model_FeatureDetail');	
	protected $_referenceMap = array(
			'feature'=>array(
					'columns' => array('featureId'),
					'refTableClass' => 'Admin_Model_Feature',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
					'onUpdate' => self::CASCADE,
			),
			'category'=>array(
					'columns' => array('categoryId'),
					'refTableClass' => 'Admin_Model_Category',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
					'onUpdate' => self::CASCADE,
			)
	);

	public function getCategoryFeature($params = null, $options = null) {
		if ($options == null) {
			$select = $this->select();
		} 
		
		$result = $this->fetchAll($select);
		if ($result) {
			return $result->toArray();
		} else {
			return null;
		}
	}
	
	public function getCategoryFeatureOne($id, $options = null) {
		if ($options == null) {
			$select = $this->select ()
				->where ( 'id = ?', ( int ) $id );
		} elseif ($options ['task'] == 'feature') {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( "{$this->_name} AS fc" )
				->join ( "feature AS f", "f.id = fc.featureId", array ('f.categoryId' ) )
				->where ( 'fd.id = ?', ( int ) $id );
		}
		
		$result = $this->fetchRow ( $select );
		if ($result) {
			return $result->toArray ();
		} else {
			return null;
		}
	}

	public function addItem($info) {
		try {
			$id = $this->insert ( $info );
			
			return $id;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function updateItem($where, $info) {
		try {
			$result = $this->update ( $info, $where);
			
			return $result;
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	public function deleteItem($params = null, $options = null) {
		try {
			if($options == null){
				$toDelete = $this->fetchAll($params);
				foreach($toDelete as $row) {
					$result = $row->delete();
				}
				
				/*
				$categoryFeatureRowset = $this->find($params);
				$categoryFeature = $categoryFeatureRowset->current();
				$result = $categoryFeature->delete();
				*/
			}elseif($options['task'] == 'delete-select'){
				foreach($params as $key => $val){
					$categoryFeatureRowset = $this->find($val);
					$categoryFeature = $categoryFeatureRowset->current();
					$result = $categoryFeature->delete();
				}
			}elseif($options['task'] == 'delete-all'){
				$params = $this->getCategoryFeature();
	
				foreach($params as $key => $val){
					$categoryFeatureRowset = $this->find($val);
					$categoryFeature = $categoryFeatureRowset->current();
					$result = $categoryFeature->delete();
				}
			}
				
			return $result;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function countItems() {
		$query = $this->select ()
			->from ( $this->_name, 'count(id) as total' );
		$result = $this->fetchRow ( $query );
		if ($result) {
			return $result->total;
		} else {
			return null;
		}
	}
}